document
		.addEventListener('DOMContentLoaded',
				function() {
					document.getElementById('loginbutton').addEventListener(
							'click', login);
					document.getElementById('loginoutbutton').addEventListener(
							'click', loginout);
					document.getElementById('addbutton').addEventListener(
							'click', add);
					document.getElementById('settingbutton').addEventListener(
							'click', setting);
					document.getElementById('searchbutton').addEventListener(
							'click', function() {
								videolist(1)
							});
					document.getElementById('searchgobutton').addEventListener(
							'click', function() {
								videolist(0)
							});
					document.getElementById('batchbutton').addEventListener(
							'click', function() {
								batchadd()
							});
					document.getElementById('checkallbutton').addEventListener(
							'click', function() {
								checkall()
							});
					document.getElementById('uncheckallbutton')
							.addEventListener('click', function() {
								uncheckall()
							});
					document.getElementById('filterKey').addEventListener(
							'keyup', function() {
								filterBykey()
							});
					document.getElementById('createcodebutton')
							.addEventListener('click', function() {
								createCode()
							});
					document.getElementById('checkCode').addEventListener(
							'click', function() {
								createCode()
							});
					document.getElementById('winCloseButton').addEventListener(
							'click', function() {
								window.close();
							});
					
					document.getElementById('addbuttonCancel').addEventListener(
							'click', function() {
								window.close();
							});
					document.getElementById('batchCancel').addEventListener(
							'click', function() {
								window.close();
							});
					document.getElementById('settingCancel').addEventListener(
							'click', function() {
								window.close();
							});

					document.getElementById('tabsName-1').addEventListener(
							'click', function() {
								selectTabs("#tabs-1");
							});
					document.getElementById('tabsName-2').addEventListener(
							'click', function() {
								//localData.setItem("Arraytitle", null);
								//localData.setItem("Arrayurl", null);
								selectTabs("#tabs-2");
							});
					document.getElementById('tabsName-3').addEventListener(
							'click', function() {
								selectTabs("#tabs-3");
							});
					document.getElementById('tabsName-4').addEventListener(
							'click', function() {
								selectTabs("#tabs-4");
							});
					document.getElementById('editSetting').addEventListener(
							'click', function() {
								selectTabs("#tabs-3")
							});

				});
document.onkeydown=function(event){ 
    e = event ? event :(window.event ? window.event : null); 
    if(e.keyCode==13){ 
        //执行的方法  
        //alert('回车检测到了'); 
    	if(document.getElementById('login_div').style.display != "none"){
    		document.getElementById('loginbutton').click();
    	}
    } 
} 

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}


/*
 * 登录
 */
function login() {
	validateResult = validate();
	if (validateResult == '1') {
		alert("请输入验证码！");
		return false;
	} else if (validateResult == '2') {
		alert("验证码输入错误！");
		return false;
	} else if (validateResult == '3') {

	}
	var str = "";
	var elem = document.forms['loginForm'].elements;
	for ( var i = 0; i < elem.length; i++) {
		if (i != 0)
			str += "&";
		str += elem[i].name + "=" + elem[i].value;
	}
	xmlhttp.onreadystatechange = loginCallback;
	xmlhttp.open("POST", "http://api.cntv.cn/client/auth", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 登录回调
 */
function loginCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var json = JSON.parse(xmlhttp.responseText);
		if (json.ret == "succ") {
			initPage();
		} else {
			alert(json.msg);
		}

	}
}

/*
 * 退出
 */
function loginout() {
	var str = "";
	xmlhttp.onreadystatechange = loginoutCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/logout", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 退出回调
 */
function loginoutCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		localData.setItem("Arraytitle", null);
		localData.setItem("Arrayurl", null);
		//alert("logout");
		//var sHref = window.location.href
		//var args = sHref.split("?");
		//window.location.href = args[0]+window.location.hash;
		//window.close();
		initPage();
	}
}

/*
 * 单片添加
 */
function add() {
	var str = "";
	var elem = document.forms['addForm'].elements;
	var cover = '';
	var sendtype = '';
	for ( var i = 0; i < elem.length; i++) {
		if ( elem[i].type == "checkbox" ) {
			if( elem[i].name == 'cover' ){
				if(elem[i].checked == true){
					cover +=","+elem[i].value;
				}
			}else if( elem[i].name == 'sendtype' ){
				if(elem[i].checked == true){
					sendtype ='1';
				}
			}

		}else if( elem[i].name != '' && elem[i].value != '' ){
			if (i != 0){
				str += "&";
			}
			str += elem[i].name + "=" + elem[i].value;
		}
		if( elem[i].name == 'title' ){
			var title = elem[i].value;
		}
		if( elem[i].name == 'tag' ){
			var tag = elem[i].value;
		}
		if( elem[i].name == 'intro' ){
			var intro = elem[i].value;
		}
		if( elem[i].name == 'album_id' ){
			var album_id = elem[i].value;
		}
		if( elem[i].name == 'special_id' ){
			var special_id = elem[i].value;
		}

	if( elem[i].name == 'cat_id' ){
			var cat_id = elem[i].value;
		}
		if( elem[i].name == 'repeat_id' ){
			var repeat_id = elem[i].value;
		}
     if( elem[i].name == 'partner_id' ){
			var partner_id = elem[i].value;
		}
    }

    if (title == '') { alert("标题不能为空！"); return; }
    if (tag == '') { alert("标签不能为空！"); return; }
    if (intro == '') { alert("简介不能为空！"); return; }
    if (cat_id == '' || cat_id == 0) { alert("分类不能为空！"); return; }
    if (repeat_id == '') { alert("马甲不能为空！"); return; }
    if (partner_id == '') { alert("合作伙伴不能为空！"); return; }

    /*
    if ((isNaN(special_id) || special_id == '') || (isNaN(album_id) || album_id == '')) {
        alert("请填写正确的专辑id和专题id！");
        return;
    }
    */
	cover = cover.substring(1);
	str +="&cover="+cover+"&sendtype="+sendtype;
	//alert(str);
	xmlhttp.onreadystatechange = addCallback;
	xmlhttp.open("POST", "http://api.cntv.cn/client/add", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 单片添加回调
 */
function addCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var json = JSON.parse(xmlhttp.responseText);
		alert(json.msg);
		//location.reload();
		selectTabs("#tabs-1");
	}
}

/*
 * 批量添加
 */
function batchadd() {
	var str = "";
	/* 获取选中的项 */
	var i = 0;
	$('.checkitem:checked').each(function() {
		if (i != 0) {
			str += "&";
		}
		url = document.getElementById("url_" + i).value;
		// title = document.getElementById("title_"+i).value;
		// alert(title);
		str += "&url[]=" + url + '';
		// str += "&title[]=" + title + '';
		i++;
	});
	var elem = document.forms['batchForm'].elements;
	var sendtype = '';
	for ( var i = 0; i < elem.length; i++) {
		if ( elem[i].type == "checkbox" ) {
			if( elem[i].name == 'sendtype' ){
				if(elem[i].checked == true){
					sendtype ='1';
				}
			}

		}else if( elem[i].name != '' && elem[i].value != '' ){
			if (i != 0){
				str += "&";
			}
			str += elem[i].name + "=" + elem[i].value;
		}

	}
	str +="&sendtype="+sendtype;
	//alert(str);

	xmlhttp.onreadystatechange = batchaddCallback;
	xmlhttp.open("POST", "http://api.cntv.cn/client/batchadd", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 全选
 */
function checkall() {
	// alert("checkall");
	var code_Values = document.getElementsByClassName("checkitem");
	for (i = 0; i < code_Values.length; i++) {
		if (code_Values[i].type == "checkbox") {
			code_Values[i].checked = true;
		}
	}
}
/*
 * 取消全选
 */
function uncheckall() {
	var code_Values = document.getElementsByClassName("checkitem");
	for (i = 0; i < code_Values.length; i++) {
		if (code_Values[i].type == "checkbox") {
			if(code_Values[i].checked == false){
				code_Values[i].checked = true;
			}else if(code_Values[i].checked == true){
				code_Values[i].checked = false;
			}
		}
	}
}
/*
 * 批量添加回调
 */
function batchaddCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		//var json = JSON.parse(xmlhttp.responseText);
		var json = eval("("+xmlhttp.responseText+")");
		alert(json.msg);
		//document.getElementById('tabsName-2').click();
		localData.setItem("Arraytitle", null);
		localData.setItem("Arrayurl", null);
		//location.reload();
		//var sHref = window.location.href
		//var args = sHref.split("?");
		//window.location.href = args[0]+window.location.hash;
		selectTabs("#tabs-2");
	}
}

/*
 * 取得马甲信息
 */
function getrepeat() {
	var str = "";
	xmlhttp.onreadystatechange = getrepeatCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/getrepeat", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 取得马甲信息回调
 */
function getrepeatCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		// alert(xmlhttp.responseText);
		// initPage();
		//var json = JSON.parse(xmlhttp.responseText);
		var json = eval("("+xmlhttp.responseText+")");
		repeat_id_select = document.getElementById("repeat_id");
		repeat_id_select.options.length = 0;
		set_repeat_id_select = document.getElementById("set_repeat_id");
		set_repeat_id_select.options.length = 0;
		if (repeat_id_select != null) {
			repeat_id_select.options.add(new Option("选择马甲", ""));
		}
		if (set_repeat_id_select != null) {
			set_repeat_id_select.options.add(new Option("选择马甲", ""));
		}
		if (json.ret == 'succ') {
			// @TODO
			$.each(json.data, function(index, d) {
				if (repeat_id_select != null) {
					repeat_id_select.options.add(new Option(d.repeat_name,
							d.repeat_id));
				}
				if (set_repeat_id_select != null) {
					set_repeat_id_select.options.add(new Option(d.repeat_name,
							d.repeat_id));
				}
			});
		} else {
			if (repeat_id_select != null) {

			}
			if (set_repeat_id_select != null) {

			}
		}
	}
}

/*
 * 取得合作伙伴信息
 */
function getpartner() {
	var str = "";
	xmlhttp.onreadystatechange = getpartnerCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/getpartner", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 取得合作伙伴信息回调
 */
function getpartnerCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		//var json = JSON.parse(xmlhttp.responseText);
		var json = eval("("+xmlhttp.responseText+")");
		partner_id_select = document.getElementById("partner_id");
		partner_id_select.options.length = 0;
		set_partner_id_select = document.getElementById("set_partner_id");
		set_partner_id_select.options.length = 0;
		if (partner_id_select != null) {
			partner_id_select.options.add(new Option("合作伙伴", ""));
		}
		if (set_partner_id_select != null) {
			set_partner_id_select.options.add(new Option("合作伙伴", ""));
		}
		if (json.ret == 'succ') {
			// @TODO
			$.each(json.data, function(index, d) {
				if (partner_id_select != null) {
					partner_id_select.options.add(new Option(d.title,
							d.pid));
				}
				if (set_partner_id_select != null) {
					set_partner_id_select.options.add(new Option(d.title,
							d.pid));
				}
			});
		} else {
			if (partner_id_select != null) {

			}
			if (set_partner_id_select != null) {

			}
		}
	}
}

/*
 * 取得分类信息
 */
function getcat() {
	var str = "";
	xmlhttp.onreadystatechange = getcatCallback;
	xmlhttp.open("GET", "http://api.xiyou.cntv.cn/general/getcategory?pid=100&lang=zh_cn&format=json", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 取得分类信息回调
 */
function getcatCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		//var json = JSON.parse(xmlhttp.responseText);
		var json = eval("("+xmlhttp.responseText+")");
		partner_id_select = document.getElementById("cat_id");
		partner_id_select.options.length = 0;
		set_partner_id_select = document.getElementById("set_cat_id");
		set_partner_id_select.options.length = 0;
		if (partner_id_select != null) {
			partner_id_select.options.add(new Option("分类", ""));
		}
		if (set_partner_id_select != null) {
			set_partner_id_select.options.add(new Option("分类", ""));
		}
		if (json.status == 'success') {
			// @TODO
			$.each(json.data, function(index, d) {
				if (partner_id_select != null) {
					partner_id_select.options.add(new Option(d.name,
							d.id));
				}
				if (set_partner_id_select != null) {
					set_partner_id_select.options.add(new Option(d.name,
							d.id));
				}
			});
		} else {

		}
	}
}

/*
 * 取得视频列表
 */
function videolist(pagenum) {
	if (pagenum == 0) {
		pagenum = document.getElementById("pagenum").value;
	}
	var status = document.getElementById("status").value;
	var str = "?status="+ status +"&format=json&page=" + pagenum + "&pagesize=20";
	//var str = "?format=json&page=" + pagenum + "&pagesize=20";
	select = document.getElementById("type");
	type = select.options[select.selectedIndex].value;
	keyword = encodeURIComponent(document.getElementById("keyword").value);
	// alert(keyword);
	if (type == "title") {
		str += "&title=" + keyword;
	} else if (type == "url") {
		str += "&url=" + keyword;
	}

	xmlhttp.onreadystatechange = videolistCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/videolist" + str, false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send();
}

function   formatDate(now)   {     
    var   year=now.getYear();     
    var   month=now.getMonth()+1;     
    var   date=now.getDate();     
    var   hour=now.getHours();     
    var   minute=now.getMinutes();     
    var   second=now.getSeconds();     
    return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;     
}     



/*
 * 取得视频列表回调
 */
function videolistCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		// alert(xmlhttp.responseText);
		//var data = JSON.parse(xmlhttp.responseText);
		var data = eval("("+xmlhttp.responseText+")");
		if(data.ret == 'succ'){
			var tab = document.getElementById("videolist");
			// alert(tab.rows.length);
			// for(var i=1;i<=tab.rows.length;i++) {
			// for(var j=1;j<tab.rows[i].cells.length;j++) {
			// var tr = tab.rows[i]; alert(i);
			// tr.parentNode.removeChild(tr);
			// }
			// }
			var table = "<table class=\"tbl lstb\">";
			table += "<tr>";
			table += "<th>状态</th>";
			table += "<th>标题</th>";
			table += "<th>时间</th>";
			table += "<th>地址</th>";
			table += "<th>备注</th>";
			table += "</tr>";
			$.each(data.data, function(index, d) {
				// 视频采集状态 10：待采集 15：采集中 20：已采集30：采集失败 35：送审中 40：已送审50：送审失败
				if (d.status == '10') {
					var statusName = "待采集";
				} else if (d.status == '15') {
					var statusName = "采集中";
				} else if (d.status == '20') {
					var statusName = "已采集";
				} else if (d.status == '30') {
					var statusName = "采集失败";
				} else if (d.status == '35') {
					var statusName = "送审中";
				} else if (d.status == '40') {
					var statusName = "已送审";
				} else if (d.status == '50') {
					var statusName = "送审失败";
				} else {
					var statusName = "未知";
				}
	
			    var   time=new   Date(d.add_time);     
				table += "<tr>";
				table += "<td  width=\"27\">" + statusName + "</td>";
				table += "<td  width=\"161\">" + d.title + "</td>";
				table += "<td width=\"70\">" + d.add_time + "</td>";
				table += "<td width=\"27\"><a style=\"color:#58a300;\" href=\"" + d.url + "\" target=\"_blank\">打开</a></td>";
				table += "<td  width=\"90\" style=\"width:90px;word-break:break-all;\">" + d.errmsg + "</td>";
				table += "</tr>";
	
				// alert(d.title);
				/*
				 * var row = tab.insertRow(-1); row.id = "row" + index; var cell1 =
				 * row.insertCell(-1); var cell2 = row.insertCell(-1); var cell3 =
				 * row.insertCell(-1); var cell4 = row.insertCell(-1); // <tr><th>状态</th><th>标题</th><th>时间</th><th>备注</th></tr>
				 * cell1.innerHTML = "<td><input type=\"checkbox\" value=\"" +
				 * d.id + "\" name=\"id\" /></td>"; cell2.innerHTML = "<td>" +
				 * d.title + "</td>"; cell3.innerHTML = "<td>" + d.add_time + "</td>";
				 * cell4.innerHTML = "<td>" + d.url + "</td>";
				 */
			})
			table += "</table>";
			tab.innerHTML = table;
	
			listinfo = "<span id=\"listtotal\">共" + data.total + "条信息</span>";
	
			if (data.currentpage > 1) {
				prePage = data.currentpage - 1;
			} else {
				prePage = 1;
			}
			if (data.currentpage >= data.totalpages) {
				nextPage = data.totalpages;
			} else {
				nextPage = data.currentpage + 1;
			}
			//listinfo += "<a href=\"?page=" + prePage
			//		+ "#tabs-4\" id=\"firstButton\">|<<</a>";
			listinfo += "<a href=\"javascript:videolist(" + prePage + ")\" id=\"firstButton\">|<<</a>";
			if (data.totalpages > 5) {
				/*
				 * if ((data.currentpage + 5) > data.totalpages) { perBlock =
				 * data.totalpages } else { perBlock = data.currentpage + 5; }
				 */
				hasMore = true;
	
				if( (data.currentpage % 5) == 0){
					currentBlock = ((data.currentpage / 5)-1) * 5 + 1;
				}else{
					currentBlock = Math.floor(data.currentpage / 5) * 5 + 1;
				}
				if ((currentBlock + 5) > data.totalpages) {
					perBlock = data.totalpages + 1;
				} else {
					perBlock = currentBlock + 5;
				}
	
				for ( var num = currentBlock; num < perBlock; num++) {
					if (data.currentpage == num) {
						//listinfo += "<a href=\"?page="
						//		+ num
						//		+ "#tabs-4\" class=\"cur\" class=\"pageButton_class\" id=\"pageButton_"
						//		+ num + "\">" + num + "</a>";
						listinfo += "<a href=\"javascript:videolist(" + num + ")\" class=\"cur\" class=\"pageButton_class\" id=\"pageButton_"
							+ num + "\">" + num + "</a>";
					} else {
						//listinfo += "<a href=\"?page="
						//		+ num
						//		+ "#tabs-4\" class=\"pageButton_class\"  id=\"pageButton_"
						//		+ num + "\">" + num + "</a>";
						listinfo += "<a href=\"javascript:videolist(" + num + ")\" class=\"pageButton_class\"  id=\"pageButton_"
							+ num + "\">" + num + "</a>";
					}
				}
			} else {
				perBlock = data.totalpages;
				hasMore = false;
				for ( var num = 1; num <= perBlock; num++) {
					if (data.currentpage == num) {
						//listinfo += "<a href=\"?page="
						//		+ num
						//		+ "#tabs-4\" class=\"cur\" class=\"pageButton_class\" id=\"pageButton_"
						//		+ num + "\">" + num + "</a>";
						listinfo += "<a href=\"javascript:videolist(" + num + ")\" class=\"cur\" class=\"pageButton_class\" id=\"pageButton_"
							+ num + "\">" + num + "</a>";
					} else {
						//listinfo += "<a href=\"?page="
						//		+ num
						//		+ "#tabs-4\" class=\"pageButton_class\"  id=\"pageButton_"
						//		+ num + "\">" + num + "</a>";
						listinfo += "<a href=\"javascript:videolist(" + num + ")\" class=\"pageButton_class\"  id=\"pageButton_"
							+ num + "\">" + num + "</a>";
					}
				}
			}
			if (data.currentpage >= data.totalpages) {
				//listinfo += "<a href=\"?page=" + nextPage
				//		+ "#tabs-4\" id=\"lastButton\">>>|</a>";
				listinfo += "<a href=\"javascript:videolist(" + num + ")\" id=\"lastButton\">>>|</a>";
			} else {
				//listinfo += " ...<a href=\"?page=" + nextPage
				//		+ "#tabs-4\" id=\"lastButton\">>>|</a>";
				listinfo += " ...<a href=\"javascript:videolist(" + num + ")\" id=\"lastButton\">>>|</a>";
			}
			/*
			 * if (perBlock > 1) { listinfo += "<a href=\"#\" id=\"firstButton\">|<<</a>"; }
			 * 
			 * for ( var num = data.currentpage; num <= perBlock; num++) { if
			 * (data.currentpage == num) { listinfo += "<a href=\"#\" class=\"cur\"
			 * id=\"pageButton_" + num + "\">" + num + "</a>"; } else { listinfo += "<a
			 * href=\"#\" id=\"pageButton_" + num + "\">" + num + "</a>"; } }
			 * 
			 * if (perBlock > 1) { if (hasMore) { listinfo += " ...<a href=\"#\"
			 * id=\"lastButton\">>>|</a>"; } else { listinfo += "<a href=\"#\"
			 * id=\"lastButton\">>>|</a>"; } }
			 */
	
			var pageInfo = document.getElementById("listinfo");
			pageInfo.innerHTML = listinfo;
	
			document.getElementById('firstButton').addEventListener('click',
					function() {
						videolist(prePage)
					});
			document.getElementById('lastButton').addEventListener('click',
					function() {
						videolist(nextPage)
					});
	
			/*
			 * if (data.totalpages > 5) { for ( var num = data.currentpage; num <=
			 * perBlock; num++) { if (data.currentpage == num) { } else {
			 * document.getElementById("pageButton_" + num)
			 * .addEventListener('click', function() { videolist(""+i+"p") });
			 * 
			 * //document.getElementById("pageButton_" +
			 * num).setAttribute("title","num"+num); } } } else { for ( var num = 1;
			 * num <= perBlock; num++) { if (data.currentpage == num) { } else {
			 * 
			 * document.getElementById("pageButton_" + num)
			 * .addEventListener('click', function() { videolist(num) });
			 * 
			 * //document.getElementById("pageButton_" +
			 * num).setAttribute("href","num"+num);
			 * //alert(document.getElementById("pageButton_" + num)); } } }
			 */
			// <span id="listtotal">共25条信息</span><a href="">|<<</a><a
			// href="">1</a><a href="" class="cur">2</a><a
			// href="">3</a><a href="">4</a><a href="">5</a> ...<a href="">>>|</a>
			// initPage();
		}else{
			var tab = document.getElementById("videolist");
			var table = "<table class=\"tbl lstb\">";
			table += "<tr>";
			table += "<th>状态</th>";
			table += "<th>标题</th>";
			table += "<th>时间</th>";
			table += "<th>地址</th>";
			table += "<th>备注</th>";
			table += "</tr>";
			table += "</table>";
			tab.innerHTML = table;
	
			listinfo = "<span id=\"listtotal\"></span>";
			var pageInfo = document.getElementById("listinfo");
			pageInfo.innerHTML = listinfo;
			
			alert(data.msg);
		}
	}
}
/*
 * 取得queryString参数
 */
function GetRequest() {
	var url = location.search; // 获取url中"?"符后的字串
	var theRequest = new Object();
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for ( var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	return theRequest;
}
/*
 * 修改表格
 */
function delTableTr(id) {
	var tab = document.getElemntsByTagId("id");
	for ( var i = 0; i < tab.rows.length; i++) {
		for ( var j = 1; j < tab.rows[i].cells.length; j++) {
			// var obj=tab.rows[i][j].getElementsByTagName("input")[0];
			// if(obj!=null && obj.type="checkbox" && obj.checked=true) {//
			// 你没说需求我就直接将第一行中有checkbox且为true的删除
			// var tr=obj.parentNode.parentNode;
			// tr.parentNode.removeChild(tr);
			// }
			tr.parentNode.removeChild(tr);
		}
	}
}

/*
 * 单片提交页面
 */
function addTab() {
	// 转载本地存储到页面表单
	//alert(localData.getItem("from"));
	if( localData.getItem("from") == 'batch' ){
		$("#title").val(localData.getItem("title"));
		$("#tag").val(localData.getItem("tag"));
		$("#intro").val(localData.getItem("intro"));
	}else{
		$("#title").val(MygetItem("title"));
		$("#tag").val(MygetItem("tag"));
		$("#intro").val(MygetItem("intro"));
	}

	getcat();
	cat_id_select = document.getElementById("cat_id");
	set_cat_id = localData.getItem("set_cat_id");
	for ( var i = 0; i < cat_id_select.options.length; i++) {
		if (cat_id_select.options[i].value == set_cat_id) {
			cat_id_select.options[i].selected = true;
		}
	}
	// $("#cat_id").val(localData.getItem("repeat_id"));
	getrepeat();
	repeat_id_select = document.getElementById("repeat_id");
	for ( var i = 0; i < repeat_id_select.options.length; i++) {
		if (repeat_id_select.options[i].value == localData.getItem("set_repeat_id")) {
			repeat_id_select.options[i].selected = true;
		}
	}
	// $("#repeat_id").val(localData.getItem("set_repeat_id"));
	getpartner();
	partner_id_select = document.getElementById("partner_id");
	for ( var i = 0; i < partner_id_select.options.length; i++) {
		if (partner_id_select.options[i].value == localData.getItem("set_partner_id")) {
			partner_id_select.options[i].selected = true;
		}
	}
	// $("#partner_id").val(localData.getItem("set_partner_id"));
	cover_checkbox = document.getElementsByClassName("cover");

	var coverAry = JSON.parse(localData.getItem("set_cover"));
	if (coverAry != null) {
		
		for ( var i = 0; i < cover_checkbox.length; i++) {
			for ( var j = 0; j < coverAry.length; j++) {
				cover_checkbox[i].checked = false;
			}
		}
		for ( var i = 0; i < cover_checkbox.length; i++) {
			for ( var j = 0; j < coverAry.length; j++) {
				if (cover_checkbox[i].value == coverAry[j]) {
					cover_checkbox[i].checked = true;
				}
			}

		}
	}
	// $("#cover").val(localData.getItem("set_cover"));
	$("#album_id").val(localData.getItem("set_album_id"));
	$("#special_id").val(localData.getItem("set_special_id"));

	$("#url").val(localData.getItem("url"));
	$("#sendtype").val(localData.getItem("sendtype"));
	$("#format").val(localData.getItem("format"));
}

/*
 * 发布设置页面
 */
function settingTab() {
	// 转载本地存储到页面表单
	//alert("set_repeat_id");
	//alert(localData.getItem("set_repeat_id"));
	//alert(localData.getItem("set_repeat_id"));
	getcat();
	cat_id_select = document.getElementById("set_cat_id");
	for ( var i = 0; i < cat_id_select.options.length; i++) {
		if (cat_id_select.options[i].value == localData.getItem("set_cat_id")) {
			cat_id_select.options[i].selected = true;
		}
	}
	// $("#cat_id").val(localData.getItem("repeat_id"));
	getrepeat();
	repeat_id_select = document.getElementById("set_repeat_id");
	for ( var i = 0; i < repeat_id_select.options.length; i++) {
		if (repeat_id_select.options[i].value == localData.getItem("set_repeat_id")) {
			repeat_id_select.options[i].selected = true;
		}
	}
	// $("#repeat_id").val(MygetItem("set_repeat_id"));
	getpartner();
	partner_id_select = document.getElementById("set_partner_id");
	for ( var i = 0; i < partner_id_select.options.length; i++) {
		if (partner_id_select.options[i].value == localData.getItem("set_partner_id")) {
			partner_id_select.options[i].selected = true;
		}
	}
	// $("#partner_id").val(localData.getItem("set_partner_id"));
	cover_checkbox = document.getElementsByClassName("set_cover");
	var coverAry = localData.getItem("set_cover");
	if (coverAry != null) {
		var coverAryJson = JSON.parse(coverAry);
		for ( var i = 0; i < cover_checkbox.length; i++) {
			for ( var j = 0; j < coverAryJson.length; j++) {
				if (cover_checkbox[i].value == coverAryJson[j]) {
					cover_checkbox[i].checked = true;
				}
			}

		}
	}

	// $("#cover").val(localData.getItem("set_cover"));
	$("#set_album_id").val(localData.getItem("set_album_id"));
	$("#set_special_id").val(localData.getItem("set_special_id"));
}

/*
 * 发布设置
 */
function setting() {
	var album_id = $("#set_album_id").val();
	var special_id = $("#set_special_id").val();
	if( (isNaN(special_id) && special_id !='') || ( isNaN(album_id)  && album_id !='' )  ){
		alert("请填写正确的专辑id和专题id！");
		return;
	}
	// 表单设置到本地存储
	// 发送给扩展模块
	cat_id_select = document.getElementById("set_cat_id");
	var cat_id = cat_id_select.options[cat_id_select.options.selectedIndex].value

	repeat_id_select = document.getElementById("set_repeat_id");
	var repeat_id = repeat_id_select.options[repeat_id_select.options.selectedIndex].value

	partner_id_select = document.getElementById("set_partner_id");
	var partner_id = partner_id_select.options[partner_id_select.options.selectedIndex].value

	if (cat_id == '' || cat_id == 0) { alert("分类不能为空！"); return; }
	if (repeat_id == '') { alert("马甲不能为空！"); return; }
	if (partner_id == '') { alert("合作伙伴不能为空！"); return; }

	var coverAry = new Array();
	cover_checkbox = document.getElementsByClassName("set_cover");
	for ( var i = 0; i < cover_checkbox.length; i++) {
		if (cover_checkbox[i].checked) {
			coverAry.push(cover_checkbox[i].value);
		}
	}
	var partner_id = partner_id_select.options[partner_id_select.options.selectedIndex].value

	/*
	localData.setItem("set_cat_id", cat_id);
	localData.setItem("set_repeat_id", repeat_id);
	localData.setItem("set_partner_id", partner_id);
	localData.setItem("set_cover", JSON.stringify(coverAry));
	localData.setItem("set_album_id", $("#set_album_id").val());
	localData.setItem("set_special_id", $("#set_special_id").val());
	alert("设置成功！");
	*/
	
	localData.setItem("set_cat_id", cat_id);
	localData.setItem("set_repeat_id", repeat_id);
	localData.setItem("set_partner_id", partner_id);
	localData.setItem("set_cover", JSON.stringify(coverAry));
	localData.setItem("set_album_id", $("#set_album_id").val());
	localData.setItem("set_special_id", $("#set_special_id").val());
	
	var str = "";
	str += "cat_id=" + cat_id;
	str += "&repeat_id=" + repeat_id;
	str += "&partner_id=" + partner_id;
	str += "&album_id=" + $("#set_album_id").val();
	str += "&special_id=" + $("#set_special_id").val();
	str += "&cover=" + JSON.stringify(coverAry);
	xmlhttp.onreadystatechange = settingCallback;
	xmlhttp.open("POST", "http://api.cntv.cn/client/setconfig", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}



/*
 * 登录回调
 */
function settingCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var json = JSON.parse(xmlhttp.responseText);
		if(json.ret == 'succ'){
			alert("设置成功！");
		}else{
			alert("设置失败！");
		}

	}
}


/*
 * 取得设置信息
 */
function getSetting() {
	var str = "";
	xmlhttp.onreadystatechange = getSettingCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/getconfig?format=json&t=" + new Date().getTime(), true);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
	//getSettingCallback();
	
}
/*
 * 取得设置信息回调
 */
function getSettingCallback() {
	//alert("getSettingCallback");
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		//var json = JSON.parse(xmlhttp.responseText);
		var json = eval("("+xmlhttp.responseText+")");
		//alert(json.status);
		if (json.ret == 'succ') {
			localData.setItem("set_cat_id", json.data.cat_id);
			localData.setItem("set_repeat_id", json.data.repeat_id);
			localData.setItem("set_partner_id", json.data.partner_id);
			localData.setItem("set_cover", JSON.stringify(json.data.cover));
			localData.setItem("set_album_id", json.data.album_id);
			localData.setItem("set_special_id", json.data.special_id);
			
			//return;
		} else {

		}
	}
}


/**
 * 框选需要
 * 
 */
function Base64() {
	// private property
	_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	// public method for decoding
	this.decode = function(input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = _keyStr.indexOf(input.charAt(i++));
			enc2 = _keyStr.indexOf(input.charAt(i++));
			enc3 = _keyStr.indexOf(input.charAt(i++));
			enc4 = _keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = _utf8_decode(output);
		return output;
	}

	// private method for UTF-8 decoding
	_utf8_decode = function(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while (i < utftext.length) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if ((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12)
						| ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}


/*
 * 批量添加页面
 */
function batchaddTab() {
	// alert("test1");
	var sHref = document.location.href
	var args = sHref.split("?");
	//alert(sHref);
	if (args[0] == sHref) /* 参数为空 */
	{
		return; /* 无需做任何处理 */
	}
	var str = args[1];
	//alert(str);
	args = str.split("&");
	var b = new Base64();
	 //alert("test2");
	var tab = document.getElementById("batchlist");
	// 如果是ie
	var table = "<table class=\"tbl lstb\" id=\"hmhm\">";
	table += "<tr>";
	table += "<th>选择</th>";
	table += "<th>标题</th>";
	table += "<th>url</th>";
	table += "</tr>";
	 //alert("test3");
	var listIndex = 0;
	for ( var i = 0; i < args.length; i++) {
		 //alert("test31");
		str = args[i];
		 //alert("test311");
		var arg = str.split("|");
		 //alert("test312");
		if (arg.length <= 1){
			continue;
		}else{
			// alert("test313");
			// addd(b.decode(arg[0]), b.decode(arg[1]));
			// alert("test32");
			table += "<tr>";
			table += "<td width=\"26\"><input name=\"checkitem\" class=\"checkitem\"  type=\"checkbox\" value=\""
					+ i + "\"/ id=\"id_" + listIndex + "\"></td>";
			table += "<td><input  type=\"hidden\" value=\"" + b.decode(arg[0])
					+ "\"/ id=\"title_" + listIndex + "\">" + b.decode(arg[0]) + "</td>";
			table += "<td width=\"100\"><input   type=\"hidden\" value=\"" + b.decode(arg[1])
					+ "\"/ id=\"url_" + listIndex + "\">" 
					+ "<div style=\"width:100px;word-break:break-all;\">"
					+ b.decode(arg[1]) 
					+ "</div>"
					+ "</td>";
			table += "</tr>";
			 //alert("test33");
			listIndex++;
		}

	}
	table += "</table>";
	 //alert("test34");
	var cat_id = localData.getItem("set_cat_id");
	var repeat_id = localData.getItem("set_repeat_id");
	var partner_id = localData.getItem("set_partner_id");
	var album_id = localData.getItem("set_album_id");
	var special_id = localData.getItem("set_special_id");
	var set_cover = localData.getItem("set_cover");
	var cover = set_cover;
			if(set_cover) cover = set_cover.replace(/\"/g,"").replace("[","").replace("]","");
	// alert("test35");
	var setDiv = "<div style='display:none;'>";
	setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
			+ cat_id + "\" />";
	setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
			+ repeat_id + "\" />";
	setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
			+ partner_id + "\" />";
	setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
			+ album_id + "\" />";
	setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
			+ special_id + "\" />";
	setDiv += "<input type=\"text\" id=\"cover\" name=\"cover\" value=\""
		+ cover + "\" />";
	setDiv += "</div>";
	tab.innerHTML = table + setDiv;
	//alert(table+setDiv);
	 //alert("test36");
	var hmnum = document.getElementById("hmnum");
	hmnum.innerText = "共" + listIndex + "条信息"; // alert(Aurl.length);
	 //alert("test4");
	
	//设置入口标示
	localData.setItem("from","batch");
}


/*
 * 批量添加页面
 */
function batchaddTab_ByLocalStore() {
	var tab = document.getElementById("batchlist");
	// 如果是ie
	if (document.all) {
		var table = "<table class=\"tbl lstb\" id=\"hmhm\">";
		table += "<tr>";
		table += "<th>选择</th>";
		table += "<th>标题</th>";
		table += "<th>url</th>";
		table += "</tr>";

		var Arraytitle = localData.getItem("Arraytitle");
		var Aurl = localData.getItem("Arrayurl");
		if (Aurl != null && Arraytitle != null && Aurl !="" && Arraytitle !="" ) {
			var ArraytitleJson = eval("("+Arraytitle+")");
			var AurlJson = eval("("+Aurl+")");
			for ( var i = 0; i < AurlJson.length; ++i) {
				table += "<tr>";
				table += "<td width=\"26\"><input  class=\"checkitem\"  type=\"checkbox\" value=\""
						+ i + "\"/ id=\"id_" + i + "\"></td>";
				table += "<td><input   type=\"hidden\" value=\""
						+ ArraytitleJson[i] + "\"/ id=\"title_" + i + "\">"
						+ ArraytitleJson[i] + "</td>";
				table += "<td width=\"100\"><input   type=\"hidden\" value=\"" + AurlJson[i]
						+ "\"/ id=\"url_" + i + "\">" 
						+ "<div style=\"width:100px;word-break:break-all;\">"
						+ AurlJson[i]
						+ "</div>"
						+ "</td>";
				table += "</tr>";
				/*
				 * var tab = document.getElementById("hmhm"); var row =
				 * tab.insertRow(-1); row.id = "row" + i; var cell1 =
				 * row.insertCell(-1); var cell2 = row.insertCell(-1); var cell3 =
				 * row.insertCell(-1); cell1.innerHTML = "<td><input
				 * type=\"checkbox\" value=\"\"/></td>"; cell2.innerHTML = "<td>" +
				 * Arraytitle[i] + "</td>"; cell3.innerHTML = "<td>" +
				 * Aurl[i] + "</td>"; num++;
				 */

			}
			table += "</table>";
			var cat_id = localData.getItem("set_cat_id");
			var repeat_id = localData.getItem("set_repeat_id");
			var partner_id = localData.getItem("set_partner_id");
			var album_id = localData.getItem("set_album_id");
			var special_id = localData.getItem("set_special_id");
			var setDiv = "<div style='display:none;'>";
			setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
					+ cat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
					+ repeat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
					+ partner_id + "\" />";
			setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
					+ album_id + "\" />";
			setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
					+ special_id + "\" />";
			setDiv += "</div>";
			tab.innerHTML = table + setDiv;
			var hmnum = document.getElementById("hmnum");
			hmnum.innerText = "共" + AurlJson.length + "条信息"; // alert(Aurl.length);
		}else{
			table += "</table>";
			tab.innerHTML = table;
		}
		// 其他浏览器
	} else {
		var table = "<table class=\"tbl lstb\">";
		table += "<tr>";
		table += "<th>选择</th>";
		table += "<th>标题</th>";
		table += "<th>url</th>";
		table += "</tr>";

		var Arraytitle = localData.getItem("Arraytitle");
		var Aurl = localData.getItem("Arrayurl");
		
		if (Aurl != null && Arraytitle != null && Aurl !="" && Arraytitle !="") {
			var ArraytitleJson = eval("("+Arraytitle+")");
			var AurlJson = eval("("+Aurl+")");
			alert(Aurl);
			for ( var i = 0; i < AurlJson.length; ++i) {
				table += "<tr>";
				table += "<td width=\"26\"><input  class=\"checkitem\"  type=\"checkbox\" value=\""
						+ i + "\"/ id=\"id_" + i + "\"></td>";
				table += "<td><input   type=\"hidden\" value=\""
						+ ArraytitleJson[i] + "\"/ id=\"title_" + i + "\">"
						+ ArraytitleJson[i] + "</td>";
				table += "<td width=\"100\"><input   type=\"hidden\" value=\"" + AurlJson[i]
						+ "\"/ id=\"url_" + i + "\">" 
						+ "<div style=\"width:100px;word-break:break-all;\">"
						+ AurlJson[i]
						+ "</div>"
						+ "</td>";
				table += "</tr>";
				/*
				 * var tab = document.getElementById("hmhm"); var row =
				 * tab.insertRow(-1); row.id = "row" + i; var cell1 =
				 * row.insertCell(-1); var cell2 = row.insertCell(-1); var cell3 =
				 * row.insertCell(-1); cell1.innerHTML = "<td><input
				 * type=\"checkbox\" value=\"\"/></td>"; cell2.innerHTML = "<td>" +
				 * Arraytitle[i] + "</td>"; cell3.innerHTML = "<td>" +
				 * Aurl[i] + "</td>";
				 */
			}
			table += "</table>";
			var cat_id = localData.getItem("set_cat_id");
			var repeat_id = localData.getItem("set_repeat_id");
			var partner_id = localData.getItem("set_partner_id");
			var album_id = localData.getItem("set_album_id");
			var special_id = localData.getItem("set_special_id");
			var setDiv = "<div style='display:none;'>";
			setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
					+ cat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
					+ repeat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
					+ partner_id + "\" />";
			setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
					+ album_id + "\" />";
			setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
					+ special_id + "\" />";
			setDiv += "</div>";
			tab.innerHTML = table + setDiv;
			var hmnum = document.getElementById("hmnum");
			hmnum.textContent = "共" + AurlJson.length + "条信息";// alert(Aurl.length)
		}else{
			table += "</table>";
			tab.innerHTML = table;
		}

	}
}

/*
 * 过滤url
 */
function filterBykey() {
	// =============================
	var filterKey = document.getElementById("filterKey").value;
	// alert("test1");
	var sHref = document.location.href
	var args = sHref.split("?");
	if (args[0] == sHref) /* 参数为空 */
	{
		return; /* 无需做任何处理 */
	}
	var str = args[1];
	args = str.split("&");
	var b = new Base64();
	// alert("test2");
	var tab = document.getElementById("batchlist");
	// 如果是ie
	var table = "<table class=\"tbl lstb\" id=\"hmhm\">";
	table += "<tr>";
	table += "<th>选择</th>";
	table += "<th>标题</th>";
	table += "<th>url</th>";
	table += "</tr>";
	// alert("test3");
	var listIndex = 0;
	for ( var i = 0; i < args.length; i++) {
		// alert("test31");
		str = args[i];
		// alert("test311");
		var arg = str.split("|");
		// alert("test312");
		if (arg.length <= 1)
			continue;
		// alert("test313");
		// addd(b.decode(arg[0]), b.decode(arg[1]));
		// alert("test32");
		if (b.decode(arg[1]).indexOf(filterKey) >= 0) {
			table += "<tr>";
			table += "<td width=\"26\"><input name=\"checkitem\" class=\"checkitem\"  type=\"checkbox\" value=\""
					+ i + "\"/ id=\"id_" + listIndex + "\"></td>";
			table += "<td><input   type=\"hidden\" value=\"" + b.decode(arg[0])
					+ "\"/ id=\"title_" + listIndex + "\">" + b.decode(arg[0]) + "</td>";
			table += "<td width=\"100\"><input   type=\"hidden\" value=\"" + b.decode(arg[1])
					+ "\"/ id=\"url_" + listIndex + "\">" 
					+ "<div style=\"width:100px;word-break:break-all;\">"
					+ b.decode(arg[1]) 
					+ "</div>"
					+ "</td>";
			table += "</tr>";
			// alert("test33");
			listIndex++;
		}
	}
	table += "</table>";
	// alert("test34");
	var cat_id = localData.getItem("set_cat_id");
	var repeat_id = localData.getItem("set_repeat_id");
	var partner_id = localData.getItem("set_partner_id");
	var album_id = localData.getItem("set_album_id");
	var special_id = localData.getItem("set_special_id");
	var set_cover = localData.getItem("set_cover");
	var cover = set_cover;
			if(set_cover) cover = set_cover.replace(/\"/g,"").replace("[","").replace("]","");
	// alert("test35");
	var setDiv = "<div style='display:none;'>";
	setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
			+ cat_id + "\" />";
	setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
			+ repeat_id + "\" />";
	setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
			+ partner_id + "\" />";
	setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
			+ album_id + "\" />";
	setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
			+ special_id + "\" />";
	setDiv += "<input type=\"text\" id=\"cover\" name=\"cover\" value=\""
		+ cover + "\" />";
	setDiv += "</div>";
	tab.innerHTML = table + setDiv;
	// alert("test36");
	var hmnum = document.getElementById("hmnum");
	hmnum.innerText = "共" + (listIndex) + "条信息"; // alert(Aurl.length);
	// alert("test4");

}



/*
 * 过滤url
 */
function filterBykey_ByLocalStore() {
	var tab = document.getElementById("batchlist");
	var filterKey = document.getElementById("filterKey").value;
	// 如果是ie
	if (document.all) {
		var table = "<table class=\"tbl lstb\" id=\"hmhm\">";
		table += "<tr>";
		table += "<th>选择</th>";
		table += "<th>标题</th>";
		table += "<th>url</th>";
		table += "</tr>";

		/*
		var Arraytitle = eval("(" + localData.getItem("Arraytitle") + ")");
		var Aurl = eval("(" + localData.getItem("Arrayurl") + ")");
		if (Aurl != null && Arraytitle != null) {
		*/
		var listIndex = 0;
		var Arraytitle = localData.getItem("Arraytitle");
		var Aurl = localData.getItem("Arrayurl");
		if (Aurl != null && Arraytitle != null && Aurl !="" && Arraytitle !="" ) {
			var ArraytitleJson = eval("("+Arraytitle+")");
			var AurlJson = eval("("+Aurl+")");
			for ( var i = 0; i < AurlJson.length; ++i) {
				if (AurlJson[i].indexOf(filterKey) >= 0) {
					table += "<tr>";
					table += "<td width=\"26\"><input  class=\"checkitem\"  type=\"checkbox\" value=\""
							+ i + "\"/ id=\"id_" + listIndex + "\"></td>";
					table += "<td><input   type=\"hidden\" value=\""
							+ ArraytitleJson[i] + "\"/ id=\"title_" + listIndex
							+ "\">" + ArraytitleJson[i] + "</td>";
					table += "<td width=\"100\"><input   type=\"hidden\" value=\"" + AurlJson[i]
							+ "\"/ id=\"url_" + listIndex + "\">" + AurlJson[i]
							+ "</td>";
					table += "</tr>";
					/*
					 * var tab = document.getElementById("hmhm"); var row =
					 * tab.insertRow(-1); row.id = "row" + i; var cell1 =
					 * row.insertCell(-1); var cell2 = row.insertCell(-1); var
					 * cell3 = row.insertCell(-1); cell1.innerHTML = "<td><input
					 * type=\"checkbox\" value=\"\"/></td>"; cell2.innerHTML = "<td>" +
					 * Arraytitle[i] + "</td>"; cell3.innerHTML = "<td>" +
					 * Aurl[i] + "</td>"; num++;
					 */
					listIndex++;
				}

			}
			table += "</table>";
			var cat_id = localData.getItem("set_cat_id");
			var repeat_id = localData.getItem("set_repeat_id");
			var partner_id = localData.getItem("set_partner_id");
			var album_id = localData.getItem("set_album_id");
			var special_id = localData.getItem("set_special_id");
			var setDiv = "<div style='display:none;'>";
			setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
					+ cat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
					+ repeat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
					+ partner_id + "\" />";
			setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
					+ album_id + "\" />";
			setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
					+ special_id + "\" />";
			setDiv += "</div>";
			tab.innerHTML = table + setDiv;
			var hmnum = document.getElementById("hmnum");
			hmnum.innerText = "共" + (listIndex) + "条信息"; // alert(Aurl.length);
		}else{
			table += "</table>";
			tab.innerHTML = table;
		}
		// 其他浏览器
	} else {
		var table = "<table class=\"tbl lstb\">";
		table += "<tr>";
		table += "<th>选择</th>";
		table += "<th>标题</th>";
		table += "<th>url</th>";
		table += "</tr>";

		/*
		var Arraytitle = JSON.parse(localData.getItem("Arraytitle"));
		var Aurl = JSON.parse(localData.getItem("Arrayurl"));
		var listIndex = 0;
		if (Aurl != null && Arraytitle != null) {
		*/
		var listIndex = 0;
		var Arraytitle = localData.getItem("Arraytitle");
		var Aurl = localData.getItem("Arrayurl");
		if (Aurl != null && Arraytitle != null && Aurl !="" && Arraytitle !="" ) {
			var ArraytitleJson = eval("("+Arraytitle+")");
			var AurlJson = eval("("+Aurl+")");
			for ( var i = 0; i < AurlJson.length; ++i) {
				if (AurlJson[i].indexOf(filterKey) >= 0) {
					table += "<tr>";
					table += "<td width=\"26\"><input  class=\"checkitem\"  type=\"checkbox\" value=\""
							+ i + "\"/ id=\"id_" + listIndex + "\"></td>";
					table += "<td><input   type=\"hidden\" value=\""
							+ ArraytitleJson[i] + "\"/ id=\"title_" + listIndex + "\">"
							+ ArraytitleJson[i] + "</td>";
					table += "<td width=\"100\"><input   type=\"hidden\" value=\"" + AurlJson[i]
							+ "\"/ id=\"url_" + listIndex + "\">" + AurlJson[i] + "</td>";
					table += "</tr>";
					/*
					 * var tab = document.getElementById("hmhm"); var row =
					 * tab.insertRow(-1); row.id = "row" + i; var cell1 =
					 * row.insertCell(-1); var cell2 = row.insertCell(-1); var
					 * cell3 = row.insertCell(-1); cell1.innerHTML = "<td><input
					 * type=\"checkbox\" value=\"\"/></td>"; cell2.innerHTML = "<td>" +
					 * Arraytitle[i] + "</td>"; cell3.innerHTML = "<td>" +
					 * Aurl[i] + "</td>";
					 */
					listIndex++;
				}
			}
			table += "</table>";
			var cat_id = localData.getItem("set_cat_id");
			var repeat_id = localData.getItem("set_repeat_id");
			var partner_id = localData.getItem("set_partner_id");
			var album_id = localData.getItem("set_album_id");
			var special_id = localData.getItem("set_special_id");
			var setDiv = "<div style='display:none;'>";
			setDiv += "<input type=\"text\" id=\"cat_id\" name=\"cat_id\" value=\""
					+ cat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"repeat_id\" name=\"repeat_id\" value=\""
					+ repeat_id + "\" />";
			setDiv += "<input type=\"text\" id=\"partner_id\" name=\"partner_id\" value=\""
					+ partner_id + "\" />";
			setDiv += "<input type=\"text\" id=\"album_id\" name=\"album_id\" value=\""
					+ album_id + "\" />";
			setDiv += "<input type=\"text\" id=\"special_id\" name=\"special_id\" value=\""
					+ special_id + "\" />";
			setDiv += "</div>";
			tab.innerHTML = table + setDiv;
			var hmnum = document.getElementById("hmnum");
			hmnum.textContent = "共" + (listIndex) + "条信息"; // alert(Aurl.length)
		}else{
			table += "</table>";
			tab.innerHTML = table;
		}
	}
}

/*
 * 取得用户信息
 */
function getuserinfo() {
	var str = "";
	xmlhttp.onreadystatechange = getuserinfoCallback;
	xmlhttp.open("GET", "http://api.cntv.cn/client/getuserinfo", false);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(str);
}
/*
 * 取得用户信息回调
 */
function getuserinfoCallback() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var username = '';
		//var json = JSON.parse(xmlhttp.responseText);
		var json = eval("("+xmlhttp.responseText+")");
		username = json.username;
		if (typeof (username) == "undefined" || username == "") {
			$("#tabs_div").hide();
			createCode();
			$("#login_div").show();
		} else {
			$("#login_div").hide();
			$("#uname").text(username);
			$("#tabs_div").show();
			// $("#tabs").tabs();
			/*
			 * $('#tabs').bind('tabsselect', function(event, ui) { // ui.tab //
			 * 被选中（点击后）的选项卡元素 // ui.panel // 这个元素包含被选中（点击后）的选项卡的内容 // ui.index //
			 * 返回一个被选中（或点击后）选项卡的索引值（从0开始） if (ui.index == '1') { addTab(); }
			 * else if (ui.index == '2') { batchaddTab(); } else if (ui.index ==
			 * '3') { settingTab(); } else if (ui.index == '4') { videolist(); }
			 * });
			 */
			// tabIndex = localData.getItem("tabs");
			// alert(window.location.hash);
			if( window.location.hash != "" ){
				selectTabs(window.location.hash);
			}else{
				selectTabs("#tabs-1");
			}
			// alert(tabIndex);
			/*
			 * if (tabIndex == '1') { selectTabs("#tabs-1"); } else if (tabIndex ==
			 * '2') { selectTabs("#tabs-2"); } else if (tabIndex == '3') {
			 * selectTabs("#tabs-3"); } else if (tabIndex == '4') {
			 * selectTabs("#tabs-4"); }
			 */
			// change_hash(tabIndex);
		}
	}
}

/*
 * 切换标签 index 选项卡标签需要：1,2...
 */
/*
 * function change_hash(index) { var tabs = $("#tabs").tabs();
 * tabs.tabs('select', index); return false; }
 */

/*
 * 初始化
 */
function initPage() {
	// 判断是否登录
	$("#tabs_div").hide();
	$("#login_div").hide();
	getuserinfo();
}

/*
 * tabs切换
 */
function selectTabs(tabIndex) {
	$("#tabs-1").hide();
	$("#tabs-2").hide();
	$("#tabs-3").hide();
	$("#tabs-4").hide();

	$("#tabsLi-1").removeClass("cur");
	$("#tabsLi-2").removeClass("cur");
	$("#tabsLi-3").removeClass("cur");
	$("#tabsLi-4").removeClass("cur");

	// alert(tabIndex);
	if (tabIndex == '#tabs-1') {
		// alert(tabIndex);
		$("#tabs-1").show();
		$("#tabsLi-1").addClass("cur");
		getSetting();
		addTab();
	}
	if (tabIndex == '#tabs-2') {
		// alert(tabIndex);
		$("#tabs-2").show();
		$("#tabsLi-2").addClass("cur");
		getSetting();
		batchaddTab();
	}
	if (tabIndex == '#tabs-3') {
		// alert(tabIndex);
		$("#tabs-3").show();
		$("#tabsLi-3").addClass("cur");
		getSetting();
		settingTab();
	}
	if (tabIndex == '#tabs-4') {
		// alert(tabIndex);
		$("#tabs-4").show();
		$("#tabsLi-4").addClass("cur");
		var Request = new Object();
		Request = GetRequest();
		page = Request['page'];
		if (!page) {
			page = 1;
		}
		videolist(page);
	}
}
/*
 * 入口程序
 */
$(document).ready(function() {
	initPage();
});

// window.setTimeout(initPage, 500);
